package com.study.d016event;

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

/**
*@description
*@author xtayhame
*date 2020年3月8日
*version
*since v1.0
*/
public class MouseEventMain2 extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox = new HBox(10);
		hBox.setPrefSize(400, 200);
		hBox.setStyle("-fx-background-color:#1ff416");
		Button b1 = new Button("button1");
		Button b2 = new Button("button2");

		TextField tf = new TextField();


		Circle circle = new Circle(40, Color.YELLOW);
		//设置点击圆形所在的区域有效(包括正方形区域)
		circle.setPickOnBounds(true);
		//阻止事件冒泡(这样设置后，点击圆形区域后，只会触发hbox区域的点击事件)
		circle.setMouseTransparent(true);

		hBox.getChildren().addAll(b1,b2,circle);
		anchorPane.getChildren().addAll(hBox);
		stage.setScene(new Scene(anchorPane, 500, 400));
		stage.show();

		/*circle.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("圆形区域被点击了.");
			}
		});

		hBox.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("hbox区域被点击了.");
			}
		});*/

		//定义一个键盘事件
		Event tfEvent = new KeyEvent(tf, tf, KeyEvent.KEY_PRESSED, "你好", "测试", KeyCode.A, false, false, false, false);

		b1.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("返回Button的相关信息：PickResult:"+event.getPickResult());
				//触发button2的鼠标点击事件
				MouseEvent.fireEvent(b2, event);
				//触发文本框的键盘按键事件
				MouseEvent.fireEvent(tf, tfEvent);

			}
		});

		b2.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("Button2.setOnMouseClicked.");
			}
		});

		tf.setOnKeyPressed(new EventHandler<KeyEvent>() {

			@Override
			public void handle(KeyEvent event) {
				System.out.println("文本框的键盘事件：getText()="+event.getText() + "   getCharacter()=" + event.getCharacter() + "   getCode()=" + event.getCode().getName());
			}
		});

	}
}
